package com.atguigu.test01;

import java.util.concurrent.*;

public class MyThreadPoolExecutor {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                5,
                30,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(9),
                Executors.defaultThreadFactory(),
                //new ThreadPoolExecutor.AbortPolicy() 直接抛异常
                //new ThreadPoolExecutor.CallerRunsPolicy() 直接抛给调用者
                //new ThreadPoolExecutor.DiscardOldestPolicy() 直接将队列中，队列头去除
                new ThreadPoolExecutor.DiscardPolicy()
                /*new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        executor.submit(r);
                    }
                }*/
        );


        for (int i = 1; i <= 20; i++) {
            String num = String.valueOf(i);
            Future<Integer> future = threadPoolExecutor.submit(() -> {
                Thread.sleep(500);
                System.out.println(Thread.currentThread().getName()+" "+num);
                return 0;
            });
        }

        threadPoolExecutor.shutdown(); //关闭线程池。
    }
}
